<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class laporan_kunjungan extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('m_laporan_kunjungan');
		$this->load->library('breadcrumb');	
	}

	public function index(){
		$this->rule->type('R');
		$this->load->library('paging'); 

		$config['sql'] = $this->m_laporan_kunjungan->get_all();
		$config['per_page'] = 6;
		$data = $this->paging->create_pagination($config);
		$judul = "Laporan Kunjungan";
		$data['judul'] = $judul;
		$this->layout->set_title($judul);
		$this->layout->set_desc("lorem ipsum");

		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Laporan', site_url('laporan_kunjungan#'));
		$this->breadcrumb->add_crumb($judul);

		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

		$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
		$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
		$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

		//untuk motong kalimat
		$this->load->helper('trim_text');

		//untuk format tanggal
		$this->load->helper('id_date');

		$this->layout->theme('apotek', 'laporan_kunjungan/index', $data);
	}

	public function search(){
		$this->rule->type('R');

		if(isset($_POST)){
			$input = $this->input->post();			

		#pagination
		$this->load->library('paging');
		$config['sql'] = $this->m_laporan_kunjungan->search($input);
		$config['per_page'] = 5;
		$data = $this->paging->create_pagination($config);
		$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');

		$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
		$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
		$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');
      
      $this->session->set_userdata('start_date', $input['keyword'][0]);
      $this->session->set_userdata('end_date', $input['keyword'][1]);

			$judul = 'Pencarian laporan kunjungan "' . $this->session->userdata('start_date') . '"sampai"'. $this->session->userdata('end_date');
			$data['judul']  = 'Pencarian laporan kunjungan " <font color="#FC771F">' . $this->session->userdata('start_date') . ' sampai ' . $this->session->userdata('end_date').'</font> "';

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

    	#import css + js
			$this->layout->set_include('css', 'themes/apotek/css/path-to-file.css');
			$this->layout->set_include('js', 'themes/apotek/css/path-to-file.js');
			$this->layout->set_include('js', 'themes/apotek/css/path-to-file.js', 'header');
	
		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Laporan', site_url('laporan_kunjungan#'));
		$this->breadcrumb->add_crumb('Kunjungan', site_url('laporan_kunjungan'));
		$this->breadcrumb->add_crumb($judul);

		$this->load->helper('trim_text');
		$this->load->helper('id_date');

		$this->layout->theme('apotek','laporan_kunjungan/index', $data);
	}else {
			redirect('kunjungan', 'refresh');
		}
	}

	public function excel($filter="", $param1="", $param2=""){
		$this->rule->type('R');
		
		#load our new PHPExcel library
  	$this->load->library('excel');
		#activate worksheet number 1
  	$this->excel->setActiveSheetIndex(0);
		#name the worksheet
  	$this->excel->getActiveSheet()->setTitle('Laporan Kunjungan');
		#set cell A1 content with some text
	   if($filter == "by_tanggal"){
			if($param1 != "null" && $param2 != "null"){
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Tanggal "'.$param1.'" s/d "'.$param2.'" '.site_title());
			}else if($param1 == "null" && $param2 == "null"){
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak '.site_title());
			}else if($param1 == "null"){
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Setelah Tanggal "'.$param2.'" '.site_title());		
			}else if($param2 == "null"){
				$this->excel->getActiveSheet()->setCellValue('A1', 'Obat Rusak Sebelum Tanggal "'.$param1.'" '.site_title());		
			}
		}else{
			$this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Kunjungan '.site_title());
		}

  	$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
  	$this->excel->getActiveSheet()->setCellValue('B2', 'Kode Kunjungan');
  	$this->excel->getActiveSheet()->setCellValue('C2', 'Nama Pasien');
  	$this->excel->getActiveSheet()->setCellValue('D2', 'Tanggal');
  	$this->excel->getActiveSheet()->setCellValue('E2', 'TD');
  	$this->excel->getActiveSheet()->setCellValue('F2', 'Suhu');
  	$this->excel->getActiveSheet()->setCellValue('G2', 'RR');
  	$this->excel->getActiveSheet()->setCellValue('H2', 'HR');
  	$this->excel->getActiveSheet()->setCellValue('I2', 'Keluhan');
		#change the font size
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		#make the font become bold
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		#merge cell A1 until D1
  	$this->excel->getActiveSheet()->mergeCells('A1:I1');
		#set aligment to center for that merged cell (A1 to D1)
  	$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		#set style
  	$TitleStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
           // 'color' => array('rgb' => '00994C') // hijau tua
            ),
  		'font'  => array(
  			'bold'  => true,
  			//'color' => array('rgb' => '000000'),
  			'size'  => 12,
  			'name'  => 'calibri'
  			)
  		);

  	$HeaderStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => '32CD32') // hijau
            ),
  		'font'  => array(
  			'bold'  => true,
  			'size'  => 12,
  			'name'  => 'calibri'
  			)
  		);

  	$ContentStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFF00') // kuning
            ),
  		'font'  => array(
  			'size'  => 12,
  			'name'  => 'calibri'
  			)
  		);

  		$this->excel->getActiveSheet()->getStyle('A1:I1')->applyFromArray($TitleStyle);
		$this->excel->getActiveSheet()->getStyle('A2:I2')->applyFromArray($HeaderStyle);
		//isi
		if($filter != "" && $keyword != "" && $keyword != " "){
			$list_data = $this->m_laporan_kunjungan->export_by($filter, $keyword);
		}else{
			$list_data = $this->m_laporan_kunjungan->export();
		}

		$cols = 3;
		$num = 1;
		foreach($list_data as $row){
			$col = $cols++;
			$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
			$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['KdKunjungan']);
			$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['Nama']);
			$this->excel->getActiveSheet()->setCellValue('D' . $col, $row['Tanggal']);
			$this->excel->getActiveSheet()->setCellValue('E' . $col, $row['TD']);
			$this->excel->getActiveSheet()->setCellValue('F' . $col, $row['Suhu']);
			$this->excel->getActiveSheet()->setCellValue('G' . $col, $row['RR']);
			$this->excel->getActiveSheet()->setCellValue('H' . $col, $row['HR']);
			$this->excel->getActiveSheet()->setCellValue('I' . $col, $row['Keluhan']);
		}

		#warna
		$cols2 = 3;
		foreach($list_data as $row){
			$col2 = $cols2++;
			$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('E' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('F' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('G' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('H' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('I' . $col2)->applyFromArray($ContentStyle);
		}


		$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("I")->setAutoSize(true);


		$filename='laporan kunjungan.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}
}

/* End of file laporan_kunjungan.php */
/* Location: ./application/controllers/laporan_kunjungan.php */